package com.telecom.at.front;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.dac.Paging;
import com.telecom.at.db.model.AvgFlow;
import com.telecom.at.db.model.BWList;
import com.telecom.at.db.model.NumOfAccount;
import com.telecom.at.db.model.SameMac;
import com.telecom.at.db.model.SigData;
import com.telecom.at.db.model.User;

@WebServlet("/front/ListSelectSigData")
public class ListSelectSigData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int date = Integer.parseInt(request.getParameter("date"));
		String city=request.getParameter("city");
		System.out.println(date);
		
		System.out.println(city);
		//date.toInteger.parseInt(s1)
		int startDate = date*10000+100;
		int endDate = date*10000+10100;
		String leftMac = request.getParameter("leftMac");
		
		String account = request.getParameter("account");
		
		if (account == null)
			account = "";

		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr == null)
			pageNoStr = "0";
		int pageNo = Integer.parseInt(pageNoStr);

		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr == null)
			pageSizeStr = "20";
		int pageSize = Integer.parseInt(pageSizeStr);

		if (request.getParameter("lastPage") != null)
			pageNo = Integer.MAX_VALUE;
		if (request.getParameter("firstPage") != null)
			pageNo = 0;
		if (request.getParameter("previousPage") != null)
			pageNo--;
		if (request.getParameter("nextPage") != null)
			pageNo++;
		
		
		List<AvgFlow> avgFlowList = null;
		List<NumOfAccount> numList = null;
		List<SigData> sigList = null;
		User curUser = (User) request.getSession().getAttribute("curUser");
		if (curUser.getId() == 0) {
			//avgFlowList = Dac.getInstance().getAvgFlowOfDate(startDate,endDate);
			if(city.equals("ȫʡ")) {
				System.out.println("123");
			sigList = Dac.getInstance().getSigDataOfDate(date);}
			else sigList = Dac.getInstance().getSigDataByCityAndDate(date,city);
			//numList = Dac.getInstance().numOfAccount();
		}
		
		else
			sigList = Dac.getInstance().getSigDataByCityAndDate(date,curUser.getCity());
		
		List<BWList>  bWList =Dac.getInstance().allBWList();
        Set<String> accountList=new HashSet<String>();
	       for(BWList bWList2:bWList)
	    	   accountList.add(bWList2.getAccount());
	       
	       List<SigData> flowList2 =new ArrayList<SigData>();
	        for(SigData avgFlow:sigList) {
				if(!accountList.contains(avgFlow.getAccount())) {
					
					flowList2.add(avgFlow);
				}
			}
		
		
	        JSONArray jsonArray = new JSONArray(flowList2);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", sigList.size());
        jsonObject.put("subjects", jsonArray);
       // System.out.println(jsonArray);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        // System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
        //SameMac sameMac = (SameMac) request.getSession().getAttribute("sameMac");
		/*Paging<SigData> paging = new Paging<SigData>(pageSize, pageNo, sigList);
		//Paging<AvgFlow> paging = new Paging<AvgFlow>(pageSize, pageNo, avgFlowList);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		request.setAttribute("paging", paging);
		request.setAttribute("account", account);
		request.setAttribute("date", date);
		request.setAttribute("basCrm3AList", avgFlowList);
		//request.setAttribute("numList", numList);			
		request.getRequestDispatcher("/WEB-INF/jsp/front/ListSelectSigData.jsp").forward(request, response);		
	*/
	}

}
